package com.caiyi.financial.nirvana.sms.mapper;

import com.caiyi.financial.nirvana.sms.base.mapper.BaseMapper;
import com.caiyi.financial.nirvana.sms.entity.SmsAuditBatch;
import com.caiyi.financial.nirvana.sms.querybean.QueryBatchMsgBean;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;
import java.util.Objects;

@Mapper
public interface SmsAuditBatchMapper extends BaseMapper<SmsAuditBatch> {


    @Update("update sms_audit_batch  set status = #{newStatus} , audit_account=#{auditAccount},audit_time=now(),reason=#{reason} where batch_id = #{batchId} and status = #{oldStatus}")
    int updateBatchAuditByBatchId(@Param("batchId") String batchId, @Param("newStatus") Integer newStatus, @Param("auditAccount") String auditAccount, @Param("reason") String reason, @Param("oldStatus") Integer oldStatus);

    @Select("select *  from sms_audit_batch where batch_id = #{batchId}")
    SmsAuditBatch selectAuditBybatchId(@Param("batchId") String batchId);


    @SelectProvider(type = BatchMsgSqlProvider.class, method = "selectBatchMsgListByParams")
    List<SmsAuditBatch> selectBatchMsgListByParams(QueryBatchMsgBean bean);

    class BatchMsgSqlProvider {
        public String selectBatchMsgListByParams(QueryBatchMsgBean bean) {
            return new SQL() {
                {
                    SELECT("id,batch_id,account,phone_count,content,notify_url,uid,ip,status,reason,audit_account,audit_time,create_time,update_time");
                    FROM("sms_audit_batch");
                    if (!StringUtils.isEmpty(bean.getBatchId())) {
                        WHERE("batch_id=#{batchId}");
                    }
                    if (!StringUtils.isEmpty(bean.getAccount())) {
                        WHERE("account=#{account}");
                    }
                    if (StringUtils.isNotBlank(bean.getContent())) {
                        WHERE("content LIKE concat('%',#{content},'%')");
                    }
                    if (!StringUtils.isEmpty(bean.getUid())) {
                        WHERE("uid=#{uid}");
                    }
                    if (Objects.nonNull(bean.getStatus())) {
                        WHERE("status=#{status}");
                    }
                    if (bean.getStartTime() != null) {
                        WHERE("create_time >= #{startTime}");
                    }
                    if (bean.getEndTime() != null) {
                        WHERE("create_time <= date_add(#{endTime}, interval 1 day)");
                    }
                    ORDER_BY("id desc");
                }
            }.toString();
        }

    }
}
